import json
import re
from curl_cffi import requests
from bs4 import BeautifulSoup
from util.getheaders import get_init_headers
import re
import os
import shutil
from util.logger import get_looger

logger = get_looger("当当网.log", __file__)

logger.info("爬虫启动......")

try:
    root = 'static/当当网/'
    if os.path.exists(root):
        shutil.rmtree(root)
    os.makedirs(root)

    url = 'https://book.dangdang.com/'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
        'referer': 'https://book.dangdang.com/'
    }
    res = requests.get(url)
    soup = BeautifulSoup(res.text, 'html.parser')
    books = []
    book_items = soup.find_all('li', attrs={'type': 'item'})

    for item in book_items:
        # 提取书名
        name_elem = item.find('p', class_='name').find('a')
        name = name_elem.get('title') if name_elem else "未知书名"

        # 提取价格 - 当前价格
        price_elem = item.find('span', class_='rob')
        if price_elem:
            price_sign = price_elem.find('span', class_='sign').get_text(strip=True) if price_elem.find('span',
                                                                                                        class_='sign') else ""
            price_num = price_elem.find('span', class_='num').get_text(strip=True) if price_elem.find('span',
                                                                                                      class_='num') else ""
            price_tail = price_elem.find('span', class_='tail').get_text(strip=True) if price_elem.find('span',
                                                                                                        class_='tail') else ""
            current_price = f"{price_sign}{price_num}{price_tail}"
        else:
            current_price = "未知价格"

        # 提取原价
        original_price_elem = item.find('span', class_='price_r')
        if original_price_elem:
            original_sign = original_price_elem.find('span', class_='sign').get_text(
                strip=True) if original_price_elem.find('span', class_='sign') else ""
            original_num = original_price_elem.find('span', class_='num').get_text(strip=True) if original_price_elem.find(
                'span', class_='num') else ""
            original_tail = original_price_elem.find('span', class_='tail').get_text(
                strip=True) if original_price_elem.find('span', class_='tail') else ""
            original_price = f"{original_sign}{original_num}{original_tail}"
        else:
            original_price = "未知原价"

        # 提取评论数量
        comment_elem = item.find('p', class_='link').find('a')
        comment_count = comment_elem.get_text(strip=True) if comment_elem else "0"

        # 提取图片链接
        img_elem = item.find('img')
        img_src = img_elem.get('src') if img_elem else ""
        img_alt = img_elem.get('alt') if img_elem else ""
        print(img_src, img_alt)
        # 提取链接
        link_elem = item.find('a', class_='img')
        product_link = link_elem.get('href') if link_elem else ""
        img_total = f'https:{img_src}'
        img_real = requests.get(img_total, headers=headers)
        logger.info(f"开始保存图片: {name}.jpg")
        clean_name = re.sub(r'[<>:"/\\|?*（）()]', '', name)
        if not clean_name.strip():
            clean_name = f"book_{len(books) + 1}"
        file_path = f"{root}{clean_name}.jpg"
        with open(file_path, "wb") as f:
            f.write(img_real.content)
        logger.info(f"保存图片成功: {name}.jpg")
        books.append({
            'name': name,
            'current_price': current_price,
            'original_price': original_price,
            'comment_count': comment_count,
            'image_src': img_src,
            'image_alt': img_alt,
            'product_link': product_link
        })

    # 打印结果
    for i, book in enumerate(books, 1):
        print(f"书籍 {i}:")
        print(f"  书名: {book['name']}")
        print(f"  当前价: {book['current_price']}")
        print(f"  原价: {book['original_price']}")
        print(f"  评论数: {book['comment_count']}")
        print(f"  图片: {book['image_src']}")
        print(f"  商品链接: {book['product_link']}")
        print("-" * 80)
except Exception as e:
    logger.error(e)

